<template>
  <div class="app-main-container">
    <!-- <div class="app-main-title">会员卡管理</div> -->
    <QueryForm v-model="filters" :query-form-config="queryFormConfig" @handleQuery="getList(1)" @clear="clearCondition" />
    <div v-if="meta.indexOf('VipCardAdd') > -1" class="doumee-table-buttons">
      <el-button v-if="meta.indexOf('VipCardAdd') > -1" type="primary" @click="handleEdit()">{{ translate("memberCardAdd") }}</el-button>
    </div>
    <el-table ref="table" v-loading="listLoading" :data="list" element-loading-text="Loading" border fit :header-row-class-name="'table-header'" class="doumee-element-table">
      <el-table-column align="center" :label="translate('membercardName')" prop="cardName" min-width="120" sortable show-overflow-tooltip />
      <el-table-column align="center" :label="translate('membercardlevel')" prop="level" min-width="110" sortable />
      <el-table-column align="center" :label="translate('shouchongMini')" prop="price" min-width="110" show-overflow-tooltip />
      <el-table-column align="center" :label="translate('xuchongMini')" prop="continuedPrice" min-width="110" show-overflow-tooltip />
      <el-table-column align="center" :label="translate('leijiShengji')" prop="totalMoney" min-width="140" />
      <el-table-column align="center" :label="translate('ky')" width="120">
        <template slot-scope="scope">
          <el-image fit="contain" :src="scope.row.showAvatar" :preview-src-list="[scope.row.showAvatar]" />
        </template>
      </el-table-column>
      <el-table-column align="center" :label="translate('saleChannel')" min-width="200" show-overflow-tooltip>
        <!-- {{ getChannel(channel) }} -->
        <template slot-scope="scope">
          <span>{{ getChannel(scope.row.channel) }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" :label="translate('morenkaka')" min-width="80">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.isnormal" active-color="#435EBE" inactive-color="#999999" :active-value="'1'" :inactive-value="'0'" :disabled="meta.indexOf('VipCardNormal') == -1" @change="toDefault(scope.row)" />
        </template>
      </el-table-column>
      <el-table-column align="center" :label="translate('autoShengji')" min-width="80">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.upgrade" active-color="#435EBE" inactive-color="#999999" :active-value="'1'" :inactive-value="'0'" :disabled="meta.indexOf('VipCardUpgrade') == -1" @change="toLevel(scope.row)" />
        </template>
      </el-table-column>
      <el-table-column align="center" :label="translate('status')" min-width="80" fixed="right">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.status" v-preventReClick active-color="#435EBE" inactive-color="#999999" :active-value="'1'" :inactive-value="'0'" :disabled="meta.indexOf('VipCardStatusEdit') == -1" @change="editStatus(scope.row)" />
        </template>
      </el-table-column>
      <el-table-column :label="translate('handle')" min-width="120" align="center" fixed="right">
        <template slot-scope="scope">
          <div class="flex-cc">
            <el-button v-if="meta.indexOf('VipCardEdit') > -1" v-preventReClick type="text" @click="handleEdit(scope.row.id)">{{ translate("edit") }}</el-button>
            <el-button v-if="meta.indexOf('VipCardDel') > -1" v-preventReClick type="text" style="color: red" @click="deleteCard(scope.row.id)">{{ translate("del") }}</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <Pagination :total="total" :page="pagination.page" @current-change="getList" @SizeChange="handleSizeChange" />
    <Edit v-if="isShowEdit" ref="EditRef" @close="isShowEdit = false" @success="getList" @sueeessAdd="pageList" />
  </div>
</template>

<script>
import { cardDefault, cardLevel, cardList, DCard, setCardStatus } from '@/api'
import { parseTime } from '@/utils/index'
import Pagination from '@/components/paginations'
import { queryFormConfig } from './config'
import Edit from './vipCardAdd'
export default {
  name: 'VipCardList',
  components: {
    Pagination,
    Edit
  },
  data () {
    return {
      isShowEdit: false,

      list: [],
      queryFormConfig,
      listLoading: true,
      meta: [],
      total: 0,
      pagination: {
        page: 1,
        rows: 10
      },
      filters: {}
    }
  },
  created () {
    this.meta = this.$route.meta.buttons || []
    this.getList(1)
  },
  mounted () {
    this.$nextTick(() => {
      this.$refs.table.doLayout()
    })
  },
  activated () {
    this.$nextTick(() => {
      this.$refs.table.doLayout()
    })
    if (this.$route.query.renovate && this.$route.query.renovate === '1') {
      this.getList()
    }
  },
  methods: {
    handleSizeChange (value) {
      this.pagination.rows = value
      this.getList(1)
    },
    pageList () {
      this.getList(1)
    },
    toDefault (v) {
      const obj = { ...v }
      const arr = this.list
      const that = this
      this.listLoading = true
      cardDefault({ param: { cardTypeId: obj.id, isNormal: obj.isnormal } }).then(res => {
        if (res.errorCode === '000000') {
          that.getList(this.pagination.page)
        } else {
          arr.forEach(e => {
            if (e.id === obj.id) {
              e.isnormal = obj.isnormal === '1' ? '0' : '1'
            }
          })
          that.list = arr
          that.listLoading = false
        }
      }).catch(() => {
        arr.forEach(e => {
          if (e.id === obj.id) {
            e.isnormal = obj.isnormal === '1' ? '0' : '1'
          }
        })
        that.list = arr
        that.listLoading = false
      })
    },
    toLevel (v) {
      const obj = { ...v }
      const arr = this.list
      const that = this
      this.listLoading = true
      cardLevel({ param: { cardTypeId: obj.id, upgrade: obj.upgrade } }).then(res => {
        if (res.errorCode === '000000') {
          that.getList(this.pagination.page)
        } else {
          arr.forEach(e => {
            if (e.id === obj.id) {
              e.upgrade = obj.upgrade === '1' ? '0' : '1'
            }
          })
          that.list = arr
          that.listLoading = false
        }
      }).catch(() => {
        arr.forEach(e => {
          if (e.id === obj.id) {
            e.upgrade = obj.upgrade === '1' ? '0' : '1'
          }
        })
        that.list = arr
        that.listLoading = false
      })
    },
    handleEdit (id) {
      this.isShowEdit = true
      this.$nextTick(() => {
        this.$refs.EditRef.isShowModal = true
        if (id) {
          this.$refs.EditRef.copName = this.translate('memberCardEdit')
          this.$refs.EditRef.getCardDetail(id)
        } else {
          this.$refs.EditRef.getStyle()
        }
      })
    },
    clearCondition () {
      // 清除条件
      this.filters = {}
      this.getList(1)
    },
    getChannel (str) {
      // 获取销售渠道
      switch (str) {
        case '0_1':
          return this.translate('shopPcDuan')
        case '1_0':
          return this.translate('wechatKehuDuan')
        case '1_1':
          return this.translate('wechatAndShop')
        default:
          break
      }
    },
    editStatus (v) {
      // 编辑卡片状态
      const obj = { ...v }
      const arr = this.list
      const that = this
      this.listLoading = true
      setCardStatus({ param: { id: obj.id, status: obj.status } }).then(res => {
        if (res.errorCode === '000000') {
          const str = obj.status === '1' ? this.translate('qy') : this.translate('jy')
          that.$message.success(str + this.translate('success'))
          that.getList(this.pagination.page)
        } else {
          arr.forEach(e => {
            if (e.id === obj.id) {
              e.status = obj.status === '1' ? '0' : '1'
            }
          })
          that.list = arr
          that.listLoading = false
        }
      }).catch(() => {
        arr.forEach(e => {
          if (e.id === obj.id) {
            e.status = obj.status === '1' ? '0' : '1'
          }
        })
        that.list = arr
        that.listLoading = false
      })
    },
    deleteCard (id) {
      // 删除卡片
      this.$confirm(this.translate('memberCardDelPlace'), this.translate('ts'), {
        confirmButtonText: this.translate('qd'),
        cancelButtonText: this.translate('cancel'),
        type: 'warning'
      })
        .then(() => {
          DCard({
            param: {
              id: id
            }
          }).then(res => {
            if (res.errorCode === '000000') {
              let totalPage = Math.ceil((this.total - 1) / this.pagination.rows)
              let currentPage = this.pagination.page > totalPage ? totalPage : this.pagination.page
              this.pagination.page = currentPage < 1 ? 1 : currentPage
              this.getList(this.pagination.page)
            }
          })
        })
        .catch(() => {
          // this.$message.info(this.translate('canceledDel'))
        })
    },
    getList (page) {
      // 获取列表
      this.listLoading = true
      const { filters, pagination } = this
      if (page) {
        pagination.page = page
        this.pagination.page = page
      }
      if (pagination.page === 1) {
        pagination.firstQueryTime = parseTime(new Date())
      }
      filters.type = '1'
      cardList({
        pagination,
        param: { ...filters }
      })
        .then((res) => {
          this.listLoading = false
          if (res.errorCode === '000000') {
            this.list = res.recordList
            this.total = res.totalCount
          }
        })
        .catch(() => {
          this.listLoading = false
        })
    }
  }
}
</script>
